//
//  RestaurantDetailViewController.m
//  ClassifyShop
//
//  Created by yuyao on 15/3/9.
//  Copyright (c) 2015年 yuyao. All rights reserved.
//

#import "RestaurantDetailViewController.h"
#import "GUIDefine.h"
#import "ScrollImageView.h"
#import "CustomImageGridView.h"
#import "MapViewController.h"
@interface RestaurantDetailViewController ()<UITableViewDataSource,UITableViewDelegate,NetWorkModalDelegate,UIAlertViewDelegate>{
    NetWorkModal *netWorkModal;
}

@property (nonatomic,strong)UITableView *myTable;
@property (nonatomic,strong)NSArray *datas;
@property (nonatomic,strong)RestaurantCore *currentRestCore;
@property (nonatomic,strong)MapViewController *mapViewController;

@end

@implementation RestaurantDetailViewController
@synthesize myTable,datas,currentRestCore,mapViewController;
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        ADDLEFTBTN(self, @"back.png", @"", @selector(back));
        ADDRIGHTBTN(self, @"share.png", @"", @selector(shareAction),20);

        myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), kAppScreenHeight-64) style:UITableViewStylePlain];
        [myTable setDelegate:self];
        [myTable setDataSource:self];
        [self.view addSubview:myTable];
        netWorkModal=[NetWorkModal sharedInstance];

    }
    
    return self;
    
}
- (void)callMethod:(id)sender{
    
    NSString *phone = [[currentRestCore.addrArray objectAtIndex:0] objectForKey:@"telephone"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"您将要拨打这个电话：%@",phone] message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
}
- (void)mapMethod:(id)sender{
    
    if (mapViewController==nil) {
        mapViewController = [[MapViewController alloc] initWithNibName:nil bundle:nil];
    }
    mapViewController.currentRestCore = self.currentRestCore;
    [self.navigationController pushViewController:mapViewController animated:YES];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        
    }else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[[currentRestCore.addrArray objectAtIndex:0] objectForKey:@"telephone"]]]];

    }
}
- (void)shareAction{
    
}
- (void)back{

    [self.navigationController popViewControllerAnimated:YES];
}
- (void)getRestaurant:(id)rc{
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    currentRestCore = (RestaurantCore *)rc;
    [myTable reloadData];
    NSLog(@"getRestaurant=%@",currentRestCore.foodsArray);
}
- (void)getRestaurantWithRestaurantId:(int)rid{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    [netWorkModal setDelegate:self];
    [netWorkModal getRestaurantWithRestId:rid];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *identifier = @"";
    
    
    
    if (indexPath.row==0) {
        
        identifier = @"cell0";
        
    }else if (indexPath.row==1){
        
        identifier = @"cell1";
        
    }else if (indexPath.row==2){
        
        identifier = @"cell2";
        
    }else if (indexPath.row==3){
        
        identifier = @"cell3";
        
    }else if (indexPath.row==4){
        
        identifier = @"cell4";
        
    }else if (indexPath.row==5){
        
        identifier = @"cell5";
        
    }else if (indexPath.row==6){
        
        identifier = @"cell6";
        
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if ([identifier isEqualToString:@"cell0"]) {
        
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
            ScrollImageView *siv = [[ScrollImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
            [siv setTag:100];
            [cell.contentView addSubview:siv];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        NSArray *foodArray = [currentRestCore foodsArray];
        
        ScrollImageView *siv = (ScrollImageView *)[cell.contentView viewWithTag:100];
        
        [siv setDatas:foodArray];
    
        
        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
        
        
        
        
        return cell;
        
        
    }else if ([identifier isEqualToString:@"cell1"]){
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UILabel *label = [UILabel new];
            [label setTag:100];
            [label setTextColor:[UIColor redColor]];
            label.lineBreakMode = NSLineBreakByCharWrapping;
            label.numberOfLines = 0 ;
            [label setTextAlignment:NSTextAlignmentCenter];
            label.font = [UIFont fontWithName:@"Arial" size:25];
            [cell.contentView addSubview:label];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
        
        [label setText:currentRestCore.name];
        
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.centerX.equalTo(cell.contentView);

        }];
        
        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
        
        
        
        
        return cell;
    }else if ([identifier isEqualToString:@"cell2"]){
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UILabel *label = [UILabel new];
            [label setTag:100];
            [label setTextColor:[UIColor redColor]];
            label.font = [UIFont fontWithName:@"Arial" size:18];
            [cell.contentView addSubview:label];
            
            
            UILabel *label2 = [UILabel new];
            [label2 setTag:101];
            [label2 setTextColor:[UIColor redColor]];
            label2.font = [UIFont fontWithName:@"Arial" size:18];
            [cell.contentView addSubview:label2];
            
            UILabel *label3 = [UILabel new];
            [label3 setTag:102];
            [label3 setTextColor:[UIColor redColor]];
            label3.font = [UIFont fontWithName:@"Arial" size:18];
            [cell.contentView addSubview:label3];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
        [label setText:@"价位:50/人"];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView).with.offset(10);

        }];
        
        UILabel *label2 = (UILabel *)[cell.contentView viewWithTag:101];
        [label2 setText:@"口味:滇味"];
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.centerX.equalTo(cell.contentView);

        }];

        
        UILabel *label3 = (UILabel *)[cell.contentView viewWithTag:102];
        [label3 setText:@"中档"];
        [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.right.equalTo(cell.contentView).with.offset(-10);
        }];
        
        
        
        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
        
        
        
        
        return cell;
    }else if ([identifier isEqualToString:@"cell3"]){
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UILabel *label = [UILabel new];
            [label setTag:100];
            [label setTextColor:[UIColor blackColor]];
            label.font = [UIFont fontWithName:@"Arial" size:15];
            [cell.contentView addSubview:label];
            
            UILabel *label1 = [UILabel new];
            [label1 setTag:101];
            [label1 setTextColor:[UIColor redColor]];
            label1.font = [UIFont fontWithName:@"Arial" size:15];
            [cell.contentView addSubview:label1];
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            [btn setTitle:@"呼叫" forState:UIControlStateNormal];
            [btn setTag:102];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cell.contentView addSubview:btn];
            
            
            UIImageView *imageView = [UIImageView new];
            [imageView setTag:103];
            [cell.contentView addSubview:imageView];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
        
        [label setText:@"电话:"];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).with.offset(10);
            make.centerY.equalTo(cell.contentView);
        }];
        
        UILabel *label1 = (UILabel *)[cell.contentView viewWithTag:101];
        
        [label1 setText:[[currentRestCore.addrArray objectAtIndex:0] objectForKey:@"telephone"]];
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(label.mas_right).with.offset(10);
            make.centerY.equalTo(cell.contentView);
        }];
        
        UIButton *btn = (UIButton *)[cell.contentView viewWithTag:102];
        [btn addTarget:self action:@selector(callMethod:) forControlEvents:UIControlEventTouchUpInside];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView.mas_right).with.offset(-10);
            make.size.mas_equalTo(CGSizeMake(70, 40));
            make.centerY.equalTo(cell.contentView);
        }];
        
        UIImageView *imageV = (UIImageView *)[cell.contentView viewWithTag:103];
        [imageV setImage:[UIImage imageNamed:@"13 呼叫.png"]];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(btn.mas_left).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(15, 20));
            make.centerY.equalTo(cell.contentView);
        }];
        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
        
        
        
        
        return cell;
    }else if ([identifier isEqualToString:@"cell4"]){
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UILabel *label = [UILabel new];
            [label setTag:100];
            [label setTextColor:[UIColor blackColor]];
            label.font = [UIFont fontWithName:@"Arial" size:15];
            [cell.contentView addSubview:label];
            
            UILabel *label1 = [UILabel new];
            [label1 setTag:101];
            label1.lineBreakMode = NSLineBreakByCharWrapping;
            label1.numberOfLines = 0 ;
            [label1 setTextColor:[UIColor blackColor]];
            label1.font = [UIFont fontWithName:@"Arial" size:15];
            [cell.contentView addSubview:label1];
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            [btn setTitle:@"地图" forState:UIControlStateNormal];
            [btn setTag:102];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cell.contentView addSubview:btn];
            
            
            UIImageView *imageView = [UIImageView new];
            [imageView setTag:103];
            [cell.contentView addSubview:imageView];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
        
        [label setText:@"地址:"];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).with.offset(10);
            make.centerY.equalTo(cell.contentView);
        }];
        
        UIButton *btn = (UIButton *)[cell.contentView viewWithTag:102];
        [btn addTarget:self action:@selector(mapMethod:) forControlEvents:UIControlEventTouchUpInside];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView.mas_right).with.offset(-10);
            make.size.mas_equalTo(CGSizeMake(70, 40));
            make.centerY.mas_equalTo(cell.contentView);

        }];
        
        UIImageView *imageV = (UIImageView *)[cell.contentView viewWithTag:103];
        [imageV setImage:[UIImage imageNamed:@"14 地图.png"]];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(btn.mas_left).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(15, 20));
            make.centerY.equalTo(cell.contentView);
        }];
        
        UILabel *label1 = (UILabel *)[cell.contentView viewWithTag:101];
        
       [label1 setText:[[currentRestCore.addrArray objectAtIndex:0] objectForKey:@"address"]];

        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(label.mas_right).with.offset(10);
            make.centerY.equalTo(cell.contentView);
            make.width.mas_equalTo(self.view.frame.size.width-150);

        }];
        CGSize size = [label1 sizeThatFits:CGSizeMake(self.view.frame.size.width-150, MAXFLOAT)];


        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, size.height+20<50?50:size.height+20);
        
        
        
        
        return cell;
    }else if ([identifier isEqualToString:@"cell5"]){
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
            UILabel *label1 = [UILabel new];
            [label1 setTag:101];
            label1.lineBreakMode = NSLineBreakByCharWrapping;
            label1.numberOfLines = 0 ;
            [label1 setTextColor:[UIColor blackColor]];
            label1.font = [UIFont fontWithName:@"Arial" size:15];
            [cell.contentView addSubview:label1];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];


        UILabel *label1 = (UILabel *)[cell.contentView viewWithTag:101];
        
        [label1 setText:[currentRestCore recommend]];
        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).with.offset(10);
            make.centerY.equalTo(cell.contentView);
            make.width.mas_equalTo(self.view.frame.size.width-20);
            
        }];
        
        CGSize size = [label1 sizeThatFits:CGSizeMake(self.view.frame.size.width-20, MAXFLOAT)];
        
        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, size.height+20<50?50:size.height+20);


        return cell;
    }else if ([identifier isEqualToString:@"cell6"]){
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
            UILabel *label1 = [UILabel new];
            [label1 setTag:101];
            label1.lineBreakMode = NSLineBreakByCharWrapping;
            label1.numberOfLines = 0 ;
            [label1 setTextColor:[UIColor blackColor]];
            label1.font = [UIFont fontWithName:@"Arial" size:15];
            [cell.contentView addSubview:label1];
            
            CustomImageGridView *gridView = [CustomImageGridView new];
            [gridView setTag:102];
            [cell.contentView addSubview:gridView];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        
        UILabel *label1 = (UILabel *)[cell.contentView viewWithTag:101];
        
        [label1 setText:[currentRestCore introduction]];
        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).with.offset(10);
            make.top.equalTo(cell.contentView).with.offset(10);
            make.width.mas_equalTo(self.view.frame.size.width-20);
            
        }];
        
        CGSize size = [label1 sizeThatFits:CGSizeMake(self.view.frame.size.width-20, MAXFLOAT)];
        
        CustomImageGridView *gridView = (CustomImageGridView *)[cell.contentView viewWithTag:102];
        [gridView setDatas:[NSArray arrayWithObjects:currentRestCore.thumb,currentRestCore.thumb,currentRestCore.thumb, nil]];
        [gridView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label1.mas_bottom);
        }];
        
        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, size.height+20+gridView.frame.size.height);
        

        return cell;
    }else{
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UILabel *label = [UILabel new];
            [label setTag:100];
            label.lineBreakMode = NSLineBreakByCharWrapping;
            label.numberOfLines = 0 ;
            label.font = [UIFont fontWithName:@"Arial" size:15];
            [cell.contentView addSubview:label];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
        [label setText:@"测试"];
        [label setFrame:CGRectMake(10, 10, 100,30)];
        cell.frame = CGRectMake(0, 0, self.view.frame.size.width, label.frame.size.height+20);

        return cell;

    }
    

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
